<!--
 * @FileDescription 标定列表
 * @Author lz
 * @Date 20230223 15:13:14
 * @LastEditors lz
 * @LastEditTime 20230223 15:13:14
-->
<template>
    <div class="demList_wp" v-if="state.show">
        <div class="tit">训练项目</div>
        <img src="/colImages/DecorativeLine_l.png" class="line">
        <img src="/colImages/packUp.png" class="packUp" @click="show()">

        <div class="menuListBox" name="scrollBox" v-if="HomeContent.Default.ModeType.value == 1">
            <div v-for="(item, index) in HomeContent.Default.MenuList.value.programs">
                <p class="Contents_p" @click="trainpro(index)"
                    :class="item.types == true ? 'Completed' : item.ctpt == true ? 'ctpt' : ''">{{
        item.Menu
    }}</p>
                <div v-for="(items, indexs) in item.steps">
                    <span class="OperateTxt"
                        :class="items.types == true ? 'BeforeOne' : items.complete == true ? 'com' : ''">{{
        items.Operate
    }}</span>
                </div>
            </div>
        </div>
        <div class="menuListBox" name="scrollBox" v-if="HomeContent.Default.ModeType.value == 2">
            <div v-for="(item, index) in HomeContent.Default.MenuList.value.programs">
                <p class="ContentsTitle" @click="trainpro(index)"
                    :class="item.types == true ? 'Completed' : item.ctpt == true ? 'ctpt' : ''">{{
        item.Menu
    }}</p>
                <div v-for="(items, indexs) in item.steps">
                    <span class="OperateTxt"
                        :class="items.types == true ? 'BeforeOne' : items.complete == true ? 'com' : ''">{{
        items.Operate
    }}</span>
                </div>
            </div>
        </div>
    </div>
    <!-- @click="SwitchingSteps(items.Id, index, indexs)">{{ items.Operate }}</span> -->

    <div class="stepsBox" v-if="HomeContent.Default.ModeType.value == 1">
        {{ HomeContent.Default.MenuList.value.programs[state.tabNum].steps[state.itemId].Description
        }}
    </div>

    <div class="StowedState" v-if="!state.show" @click="show()">
        <img src="/colImages/ExpandArrow_l.png" class="expArr">
    </div>
    <messageBox ref="messageBoxRef" :Questions="'所有训练步骤皆已完成，请返回上级目录重新选择训练。'" :messageType="4" :ID="NaN"></messageBox>
</template>

<script setup lang="ts">
import { ClientMessage } from '@runtime/generate/message/ClientMessage';
import WebSocketComponent from '@runtime/module/WebSocket/WebSocketComponent';
import { reactive, ref, toRefs, onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onUnmounted, StyleValue, defineProps, defineEmits, watch, h } from 'vue'
import HomeContent from '../../HomeContent';
import messageBox from '/src/components/Apps/Simulation/messageBox.vue'
import { message, Modal } from 'ant-design-vue';
import { any } from 'vue-types';
const state = reactive({
    show: true,
    tabNum: 0,
    itemId: 0,
});
function show() {
    state.show = !state.show
}
const scrollList = document.getElementsByName('scrollBox')


async function scrollThen(x: any) {
    if ((x.clientHeight / x.scrollHeight) < 0.8) {
        if (x.scrollHeight - x.scrollTop === x.clientHeight) {
            x.scrollTop = 0
        } else {
            x.scrollTop += 20
        }
    }
}
//项目切换
async function trainpro(val: any) {
    if (HomeContent.Default.ModeType.value == 2) {
        BeforeTwo()
        console.log(val);
        state.tabNum = val
        state.itemId = 0
        HomeContent.Default.MenuList.value.programs[val].ctpt = true
        SendTopicMessage(0)
    }
}
function SendTopicMessage(index: number) {
    BeforeOne()
    HomeContent.Default.MenuList.value.programs[state.tabNum].steps[state.itemId].types = true
    console.log('调用发送步骤Id方法');

    if (HomeContent.Default.ModeType.value == 1) {
        //教学模式
        let ID = HomeContent.Default.MenuList.value.programs[state.tabNum].steps[index].Id
        let newArr = WebSocketComponent.Default.PostMessageAsync(new ClientMessage.NeolithTrainStepRequest(ID))
        console.log(newArr, '训练项目回调')
    } else if (HomeContent.Default.ModeType.value == 2) {
        //训练模式
        let ID = HomeContent.Default.MenuList.value.programs[state.tabNum].steps[index].Id
        let newArr = WebSocketComponent.Default.PostMessageAsync(new ClientMessage.NeolithTrainRequest(ID))
        console.log(newArr, '训练项目回调')
    }
}
function SwitchingSteps(Id: number, index: number, indexs: number) {
    state.tabNum = index
    state.itemId = indexs
    if (HomeContent.Default.ModeType.value == 1) {
        //教学模式
        let newArr = WebSocketComponent.Default.PostMessageAsync(new ClientMessage.NeolithTrainStepRequest(Id))
        console.log(newArr, '训练项目回调')
    } else if (HomeContent.Default.ModeType.value == 2) {
        //训练模式
        let newArr = WebSocketComponent.Default.PostMessageAsync(new ClientMessage.NeolithTrainRequest(Id))
        console.log(newArr, '训练项目回调')
    }
}

//添加步骤标识
function BeforeOne() {
    HomeContent.Default.MenuList.value.programs.forEach((item: any) => {
        item.steps.forEach((items: any) => {
            items.types = false
        });
    });
}

//添加步骤标识
function BeforeTwo() {
    HomeContent.Default.MenuList.value.programs.forEach((item: any) => {
        item.ctpt = false
    });
}

//监听步骤完成推送
watch(() => HomeContent.Default.RelaySequence.value, (newVal, oldVal) => {

    if (newVal != oldVal) {
        //发送步骤信息
        if (HomeContent.Default.ModeType.value == 1) {
            console.log('HomeContent.Default.MenuList.value.programs', HomeContent.Default.MenuList.value.programs);

            //教学模式
            if (HomeContent.Default.MenuList.value.programs[state.tabNum].steps[state.itemId + 1]) {
                HomeContent.Default.MenuList.value.programs[state.tabNum].steps[state.itemId].complete = true
                console.log('下一步');
                state.itemId++
                scrollThen(scrollList[0])
                SendTopicMessage(state.itemId)
            } else {
                HomeContent.Default.MenuList.value.programs[state.tabNum].steps[state.itemId].complete = true
                console.log('已完成');
                message.success('当前训练项目已完成！');
                //判断是否为最后训练项
                if (HomeContent.Default.MenuList.value.programs[state.tabNum + 1]) {
                    state.tabNum++
                    state.itemId = 0
                    SendTopicMessage(0)
                } else {
                    HomeContent.Default.MenuList.value.programs[state.tabNum].steps[HomeContent.Default.MenuList.value.programs[state.tabNum].steps.length - 1].types = false
                    Out()
                }

            }


        } else if (HomeContent.Default.ModeType.value == 2) {
            BeforeTwo()
            HomeContent.Default.MenuList.value.programs[state.tabNum].types = true
            //训练模式
            message.success('当前训练项目已完成！');
        }
    }
})

//考核完成监听
watch(() => HomeContent.Default.NeolithTrainMessage.value, (newVal, oldVal) => {
    if (newVal != oldVal) {
        //发送步骤信息
        BeforeTwo()
        HomeContent.Default.MenuList.value.programs[state.tabNum].types = true
        //训练模式
        message.success('当前训练项目已完成！');
    }
})
watch(() => HomeContent.Default.vehicleStart.value, (newVal, oldVal) => {
    if (newVal != oldVal) {
        console.log(newVal, oldVal);
        console.log('HomeContent.Default.vehicleStart.value', HomeContent.Default.vehicleStart.value);
        if (!HomeContent.Default.vehicleStart.value) {
            return
        } else {
            message.warning(HomeContent.Default.vehicleStart.value);
        }
        HomeContent.Default.vehicleStart.value = ''
    }
})

const messageBoxRef = ref<any>();

function Out() {
    messageBoxRef.value.showModel()
}
// const success = () => {
//     Modal.success({
//         title: () => '训练完成  ',
//         content: () => h('div', {}, [
//             h('p', '所有训练步骤皆已完成'),
//             h('p', '请返回上级目录重新选择训练'),
//         ]),
//     });
// };
//监听考核完成回调
watch(() => HomeContent.Default.NeolithExamMessage.value, (newVal, oldVal) => {
    if (newVal != oldVal) {
        message.success(`用时${HomeContent.Default.NeolithExamMessage.value.time}秒`);
        message.success(`偏离得分${HomeContent.Default.NeolithExamMessage.value.offset}分`);
        message.success(`碰撞得分${HomeContent.Default.NeolithExamMessage.value.collision}分`);
    }
})

onMounted(() => {
    //进入默认发消息
    SendTopicMessage(0)
    HomeContent.Default.MenuList.value.programs[0].ctpt = true
})
</script>

<style scoped>
.demList_wp {
    position: absolute;
    left: 0;
    top: 108px;
    width: 290px;
    height: 637px;
    background-image: url('/colImages/bdxm_bg.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

.line {
    position: absolute;
    left: 6px;
    top: 32px;
    width: 274px;
    height: 28px;
}

.tit {
    font-size: 22px;
    font-family: YouSheBiaoTiHei;
    font-weight: 400;
    color: #ffffff;
    padding-left: 38px;
    padding-top: 12px;
}

.packUp {
    position: absolute;
    right: 4px;
    top: 13px;
    width: 27px;
    height: 32px;
}

.StowedState {
    position: absolute;
    left: 0;
    top: 108px;
    width: 52px;
    height: 52px;
    background-image: url('/colImages/stowedState_l_bg.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

.expArr {
    width: 27px;
    height: 32px;
    margin-top: 10px;
    margin-left: 13px;
}

.menuListBox {
    margin-top: 10px;
    margin-left: 10px;
    height: 570px;
    width: 270px;
    overflow-y: auto;
    color: #fff;
}

.menuListBox::-webkit-scrollbar {
    /*滚动条整体样式*/
    width: 10px;
    /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
}

.menuListBox::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    background: #66E0FF;
}

.menuListBox::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    border-radius: 10px;
    background: #165A9A;
}

.ContentsTitle {
    font-size: 16px;
    margin: 0;
    line-height: 28px;
    margin-left: 20px;
    color: #B1D0F4;
}

.ContentsTitle:hover {
    color: #fff;
}

.Contents_p {
    font-size: 16px;
    margin: 0;
    line-height: 28px;
    margin-left: 20px;
    color: #B1D0F4;
}

.SubmenuTxt {
    font-size: 16px;
}

.stepsBox {
    /* width: 880px; */
    /* height: 40px; */
    /* background-image: url('/colImages/CElist_bg.png');
    background-repeat: no-repeat;
    background-size: 100% 100%; */
    position: absolute;
    text-align: center;
    line-height: 40px;
    top: 10%;
    left: 50%;
    transform: translate(-50%, 0);
    font-size: 16px;
    color: #fff;
    background-color: rgba(57, 118, 183, 0.8);
    border-radius: 10px;
    padding: 0 10px;
    border: 1px solid rgba(0, 230, 245);
}

.DescriptionBox {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, 0);
    font-size: 20px;
    color: #fff;
    background-color: rgb(14, 52, 61);
}

.OperateTxt {
    font-size: 16px;
    line-height: 24px;
    margin-left: 35px;
    color: #B1D0F4;
}

.OperateTxt:hover {
    /* color: #fff; */
}

.ctpt {
    color: #fff;
}

.BeforeOne {
    color: #fff !important;
}

.Completed {
    color: #4CE223;
}

.com {
    color: #4CE223;
}
</style>